package com.robert.autoplayvideo;

import com.robert.autoplayvideo.slice.AutoPlayAbilitySlice;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;
import ohos.agp.window.dialog.ToastDialog;

import java.util.ArrayList;
import java.util.List;

public class AutoPlayAbility extends Ability {
    private ListContainer mListContainer;
    private VideoItemProvider itemProvider;
    private ArrayList<VideoModel> modelList = new ArrayList<>();
    private boolean isFirst=true;
    private List<Integer> indexList=new ArrayList<>();
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
//        super.setMainRoute(AutoPlayAbilitySlice.class.getName());
        super.setUIContent(ResourceTable.Layout_ability_auto_play);
        modelList=getIntent().getSequenceableArrayListParam("modelList");
        if(modelList==null||modelList.size()==0){
            new ToastDialog(this).setContentText("列表数据为空，请重试").show();
            return;
        }
        mListContainer= (ListContainer) findComponentById(ResourceTable.Id_listView);
        itemProvider=new VideoItemProvider(this,modelList);
        itemProvider.setDownloadVideos(true);
        mListContainer.setItemProvider(itemProvider);
        System.out.println("onScrollFirst==>>>"+getFirstShowViewToPlayer());
        mListContainer.setScrollListener(new ListContainer.ScrollListener() {
            @Override
            public void onScrollFinished() {
                System.out.println("onScrollFinished==>>>"+getFirstShowViewToPlayer());
            }
        });
        mListContainer.addItemVisibilityChangedListener(new ListContainer.ItemVisibilityChangedListener() {
            @Override
            public void onItemAdded(Component component, int i) {
                indexList.add(i);
            }

            @Override
            public void onItemRemoved(Component component, int i) {
            }
        });
    }
    private int getFirstShowViewToPlayer(){
        if(indexList.isEmpty()){
            return 0;
        }
        if(isFirst){
            isFirst=false;
            return 0;
        }
        return indexList.get(indexList.size()-2);
    }

}
